package com.yingjia.yingjiauniversity.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.yingjia.yingjiauniversity.R;
import com.yingjia.yingjiauniversity.utils.ActivityStack;

import io.reactivex.disposables.CompositeDisposable;

public class BaseActivity extends RxAppCompatActivity implements MvpView, BaseFragment.Callback {
    protected CompositeDisposable compositeDisposable;

    private BroadcastReceiver mConnectReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(final Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                boolean isDisconnected = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (isDisconnected) {
                    showNetworkDisconnectView();
                } else {
                    hideNetworkDisconnectView();
                }
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        compositeDisposable = new CompositeDisposable();
        if (savedInstanceState != null) {

        }
        ActivityStack.getInstance().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter connectFilter = new IntentFilter();
        connectFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mConnectReceiver, connectFilter);
    }

    @Override
    protected void onStart() {
        translucentStatusBar();
        super.onStart();
    }

    public void translucentStatusBar() {
        if (Build.VERSION.SDK_INT >= 19) {
            ViewGroup contentLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
            if (contentLayout != null) {
                View view = contentLayout.getChildAt(0);
                if (view != null) {
                    view.setFitsSystemWindows(true);
                }
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mConnectReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mConnectReceiver = null;
        compositeDisposable.clear();
        ActivityStack.getInstance().finishActivity(this);
    }

    @Override
    public void onFragmentAttached() {

    }

    @Override
    public void onFragmentDetached(String tag) {

    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (outState == null) {
            outState = new Bundle();
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.fontScale = 1.0f;
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    protected void setStatusBar() {

    }

    @Override
    public void showToast(String message) {

    }

    @Override
    public void showToast(@StringRes int resId) {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    public void showLoading(boolean cancelable, Object tag) {
        hideLoading();
    }

    @Override
    public void hideLoading() {
    }

    /**
     * 网络断开
     */
    public void showNetworkDisconnectView() {
    }

    /**
     * 网络连接
     */
    public void hideNetworkDisconnectView() {
    }
}
